require("TSLib")

local color = require("buyer_color")
local toucher = require("toucher")
local logger = require("logger")
local config = require("config")
local M = {}

local can_buy_value = 501--config.minimum_buy_60

function M.open_market(...)
	-- body
end

function M.open_60(...)
	
	toucher.init_top(100,200);

	M.next_page();
	mSleep(2000)

	--common.log("寻找图中是否存在珍品召唤兽_未选择的按钮");
	x, y = color.find_pet();

	mSleep(2000)
	if x > 0 and y > 0 then
		logger.warn("slide down failed.")
		M.next_page()
	end
	
	-- 打开【任务道具】
	M.open_quests();
	-- 


	mSleep(1000)
	-- 在任务道具中找色【60级普通装备】
	x, y = color.find_60_quests()
	
	if x > 0 and y > 0 then
		logger.debug("发现目标图标:"..x..","..y)
		mSleep(1000)
		toucher.click(x,y)
	else
		logger.debug("未发现目标图标");
	end
end

function M.open_50(...)
	
	toucher.click(100,450)
	mSleep(1000)
	toucher.click(150,500)
	mSleep(1000)
	toucher.init_top(100,200)
	mSleep(1000)
	M.next_page()
	mSleep(2000)

	--common.log("寻找图中是否存在珍品召唤兽_未选择的按钮");
	x, y = color.find_pet()

	mSleep(2000)
	if x > 0 and y > 0 then
		logger.warn("slide down failed.")
		M.next_page()
	end
	
	-- 打开【任务道具】
	M.open_quests();
	-- 


	mSleep(1000)
	-- 在任务道具中找色【60级普通装备】
	x, y = color.find_50_quests()
	
	if x > 0 and y > 0 then
		logger.debug("发现目标图标:"..x..","..y)
		mSleep(1000)
		toucher.click(x,y)
		return true
	else
		logger.debug("未发现目标图标");
		return false
	end
end

function M.open_50_bak(...)
	mSleep(1000)
	-- 在任务道具中找色【50级普通装备】
	x,y = color.find_50_quests()

	if x > 0 and y > 0 then
		logger.debug("发现目标图标:"..x..","..y)
		mSleep(1000)
		toucher.click(x,y)
	else
		logger.debug("未发现目标图标")
	end
end


function M.open_quests(...)		
	-- 寻找未选中的【任务道具】按钮
	x, y = color.find_quests();


	if x > 0 and y > 0 then
		logger.debug("发现目标图标:"..x..","..y)
		toucher.click(x,y)
	else
		logger.debug("未发现目标图标")
	end
end

function M.get_50_gold(...)
	x, y = color.find_gold(400);
	if x > 0 and y > 0 then
		return 400, x, y;
	end
	x, y = color.find_gold(450);
	if x > 0 and y > 0 then
		return 450, x, y;
	end
	x, y = color.find_gold_500();
	if x > 0 and y > 0 then
		return 500, x, y;
	end
	x, y = color.find_gold(550);
	if x > 0 and y > 0 then
		return 550, x, y;
	end
	x, y = color.find_gold(600);
	if x > 0 and y > 0 then
		return 600, x, y;
	end
	
	x, y = color.find_gold(650);
	if x > 0 and y > 0 then
		return 650, x, y;
	end
	x, y = color.find_gold(700);
	if x > 0 and y > 0 then
		return 700, x, y;
	end
	
	x, y = color.find_gold(750);
	if x > 0 and y > 0 then
		return 750, x, y;
	end
	return 99999999,-1,-1;
end

function M.get_gold(...)
	x, y = color.find_gold_1200();
	if x > 0 and y > 0 then
		return 1200, x, y;
	end
	x, y = color.find_gold_1350();
	if x > 0 and y > 0 then
		return 1350, x, y;
	end
	x, y = color.find_gold_1500();
	if x > 0 and y > 0 then
		return 1500, x, y;
	end
	x, y = color.find_gold_1650();
	if x > 0 and y > 0 then
		return 1650, x, y;
	end
	x, y = color.find_gold_1800();
	if x > 0 and y > 0 then
		return 1800, x, y;
	end
	
	x, y = color.find_gold_1950();
	if x > 0 and y > 0 then
		return 1950, x, y;
	end
	x, y = color.find_gold_2100();
	if x > 0 and y > 0 then
		return 2100, x, y;
	end
	
	x, y = color.find_gold_2250();
	if x > 0 and y > 0 then
		return 2150, x, y;
	end
	return 99999999,-1,-1;
end

function M.next_page(...)
	touchDown(50, 350+50)
	touchMove(50, 350)
	touchUp(50, 351)
	mSleep(1000)
end



function M.open_60_quests(...)
	-- body
end

function function_name(...)
	-- body
end

function M.switch(idx, is_init)
	x, y = color.find_select_quests()
	mSleep(1000)
	if x == -1 and y == -1 then
		logger.info("click tabs(390.580).")
		toucher.click(390,580)
		mSleep(1000)
	end
	if is_init ~= nil and is_init then
		toucher.init_top(370,450)
	end
	for i = 1, idx, 1 do
		toucher.slide_down_tab(370,450)
	end
	mSleep(1000)
	-- 点击当前tab中的第一位
	toucher.click(400,380)
	mSleep(1000)
end

function M.buying(x, y)
	toucher.click(x,y)
	mSleep(1000)
	x, y = color.find_buy_button()
	mSleep(1000)
	logger.info("buying"..x..","..y)
	if x ~= -1 and y ~= -1 then
		logger.info("buying"..x..","..y)
		toucher.click(x, y)
		mSleep(1000)
	else
		logger.warn("not find buy button.")
	end
end

function M.process(...)
	if M.open_50()~=true then
		return
	end
	mSleep(1000);
	
	M.switch(0,true)
	for i = 0, 12, 1 do
		value,x,y = M.get_50_gold();
		logger.info("the equipment value: "..value);
		mSleep(1000);
		if value < can_buy_value and x ~= -1 and y ~= -1 then
			logger.info("buy the equipment of "..i)
			M.buying(x,y)
			mSleep(1000)
			x, y = color.find_no_gold()
			if x ~= -1 and y ~= -1 then
				logger.warn("the account money not enough.")
				return false
			end
		end
		M.switch(1)
	end
end

init(1);
for i = 0, 100000,1 do
	M.process()
end

return M;